home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2149 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: vietual data in C++?
  5. Date: 16 Jan 1996 02:24:14 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4df28e$2i7@colossus.holonet.net>
  8. References: <4d94cn$9vo@fnnews.fnal.gov>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Oleg Krivosheev (kriol) wrote:
  13. : Hi, Gentlemen
  14.  
  15. : i want to implement something like virtual data (if such an idiom
  16. : is already known, there must be it's own name)...
  17.  
  18. Look up "virtual data" in the c.l.c++ FAQ; you'll find a very
  19. interesting discussion of this idiom.  It's useful when objects 
  20. in one class hierarchy need to contain (or, as it's implemented,
  21. point to) a member of a different hierarchy.  It's not clear to
  22. me that your situation falls into this category. 
  23.  
  24. [code deleted]
  25.  
  26. : 2. What's wrong with above snippet?
  27.  
  28. In an earlier post, David Bryden pointed out some dubious things
  29. in your code.  Let me add I'm not happy with your idea of deriving
  30. unrelated classes from a struct.  This is using an OOP construct
  31. for a very non-OO purpose, and the result will be an unmaintainable
  32. C/C++ mishmash.  It would be better to code the struct as a member
  33. of the classes, and IMO even better to abandon the struct and code
  34. meaningful data members for each class, separately.  (And make them
  35. private!)  Inheritance doesn't seem appropriate here. 
  36.  
  37. As long as you use a template, the question of inheritance is moot
  38. anyway, and "virtual data" doesn't apply.  For example, you could 
  39. have the Base template contain an object, rather than a pointer:
  40.  
  41.      template <class T> Base { ...
  42.         T myinstance; // instead of T* pVirtualData;
  43.      };
  44.  
  45. Then Base<Data1> and Base<Data2> would work fine as base classes
  46. for D1 and D2, if you want, and you could write:
  47.  
  48.      D1 d1;
  49.      cerr << d1.myinstance.i1; 
  50.  
  51. (Of course, you'd do this differently since you took my advice
  52. and made i1 private, right? :)
  53.  
  54. BTW, if Base doesn't have to be a template for other reasons,
  55. and you want to keep the BaseClass/Data1/Data2... hierarchy (ugh),
  56. then perhaps the "virtual data" idiom would work in lieu of a
  57. template.  You definitely don't need both.
  58.  
  59. Good luck.
  60. --
  61. Russell Blackadar,   russell@mdli.com
  62.